<?PHP

/*
 * Copyright (C) 2011 Joachim Barthel
 */

class WebAds {

// var $dbh = 0;	//db-handle

	
    function openBanner($cStyle,$cMode)
    {
        include '../../config.inc.php';

        echo '<!DOCTYPE  HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
        echo '<html>';
        echo '<head>';
        echo '<title>Anzeige</title>';
        if (substr($cMode,0,1) == 'r')
            echo '<meta http-equiv="refresh" content="'.substr($cMode,1).'">';
        echo '<link rel="stylesheet" type="text/css" href="'.$this->sShopURL.'/modules/oxads/'.$cStyle.'.css">';
        /*echo '<script type="text/javascript">';
        echo 'function enableOxAds ()';
        echo '{';
        echo 'Timer = setTimeout(\'reloadOxAds()\' , 2000);';
        echo '}';
        echo 'function reloadOxAds ()';
        echo '{';
        echo 'alert(\'reloadOxAds\');';
        echo 'document.getElementById("adtable").innerHTML = "abc"';
        //echo 'Timer = setTimeout(\'reloadOxAds()\' , 3000);';
        echo '}';
        echo '</script>';*/
        echo '</head>';
        echo '<body class="oxad">';
    }
	
	
    function closeBanner()
    {
            echo '</body></html>';
    }


    function openDB()
    {
        include '../../config.inc.php';

        $dbConn = 'mysql:host='.$this->dbHost.';port=3306;dbname='.$this->dbName;
        $dbUser = $this->dbUser;
        $dbPass = $this->dbPwd;

        $dbh = new PDO($dbConn, $dbUser, $dbPass); 
        //$dbh->exec('set names "utf8"');

        if (!empty($dbh)) 
            return $dbh;
        else
            return 0;
    }


    function getActionArticles($dbh,$actionID)
    {
        include '../../config.inc.php';
        
        $sql = 'SHOW COLUMNS FROM oxactions WHERE field=\'OXTYPE\'';
        $stmt = $dbh->prepare($sql);
        $stmt->execute();
        if ($stmt->rowCount() != 0)
            $fieldOXTYPE = 'oxactions.oxtype,';
        else {
            $fieldOXTYPE = '';
        }

        $sql = 'SELECT '.$fieldOXTYPE.' oxarticles.oxid, oxarticles.oxparentid, oxarticles.oxtitle, '
             . 'oxarticles.oxshortdesc, oxarticles.oxprice, oxarticles.oxtprice, oxarticles.oxvarminprice, '
             . 'oxarticles.oxvarcount, oxarticles.oxicon, oxarticles.oxpic1 '
             . 'FROM oxactions,oxactions2article,oxarticles '
             . 'WHERE oxactions.oxid=oxactions2article.oxactionid '
             . 'AND oxarticles.oxid=oxactions2article.oxartid '
             . 'AND oxactions.OXID=\''.$actionID.'\' '
             . 'ORDER BY oxactions2article.oxsort ';
//echo $sql;
        $stmt = $dbh->prepare($sql);
        $stmt->execute();
        $dbData = $stmt->fetchAll();
        if (count($dbData) == 0)
            echo $sql;
        return $dbData;
//return $this;
    }

    function formatAdvertisement($artList,$adBanner)
    {
        include '../../config.inc.php';
        //include 'adsizes.inc.php';

        if (count($artList) == 0)
            return;
        
        switch ($adBanner) {
            case 'LargeBanner' :
                $tableStyle = 'horizontal';
                $tableHeight = 90;
                $tableWidth = 720;
                break;
            case 'MidBanner' :
                $tableStyle = 'horizontal';
                $tableHeight = 90;
                $tableWidth = 460;
                break;
            case 'WideColumn' :
                $tableStyle = 'vertical';
                $tableHeight = 610;
                $tableWidth = 190;
                $headHeight = 40;
                $rowHeight = 98;
                $maxArticles = 5;
                break;
            case 'SmallColumn' :
                $tableStyle = 'vertical';
                $tableHeight = 500;
                $tableWidth = 160;
                $headHeight = 40;
                $rowHeight = 98;
                $maxArticles = 4;
                break;
        }

        echo '<div class="adhint">Anzeige</div>';
        echo '<div class="maintable" id="adtable" style="height:'.$tableHeight.'px; width:'.$tableWidth.'px;">';

        if ($tableStyle == 'vertical')
            {
            echo '<div class="header" style="height:'.$headHeight.'px;">OXID eShop</div >';
            $rowCount = 0;
            foreach($artList as $article) {
                $rowCount++;
            }
            $iNum = array();
            for ($i=0; $i<$maxArticles; $i++) {
                do {
                    $j = rand(0,$rowCount-1);
                } while (!(array_search($j,$iNum) === FALSE));
                array_push($iNum, $j);
                $article = $artList[$j];
                echo '<div class="space"> </div>';
                echo '<div style="height:'.$rowHeight.'px;">';
                if (empty($article['oxicon']))
                    $article['oxicon'] = substr($article['oxpic1'],0,strlen($article['oxpic1'])-4).'_ico'.substr($article['oxpic1'],-4);
                echo '<div class="image" style="height:'.$rowHeight.'px;width:60px;">'
                   . '<a href="'.$this->sShopURL.'/index.php?cl=details&anid='.$article['oxid'].'" target="_blank">'
                   . '<img src="'.$this->sShopURL.'/out/pictures/icon/'.$article['oxicon'].'">'
                   . '</a></div>';
                echo '<div class="description" style="height:'.$rowHeight.'px;width:'.($tableWidth-65).'px;">'
                   . '<div class="title">'
                   . '<a href="'.$this->sShopURL.'/index.php?cl=details&anid='.$article['oxid'].'" target="_blank">'.$article['oxtitle'].'</a>'
                   . '</div>';
                if (!empty($article['oxshortdesc']))
                    echo '<div class="desc">'.$article['oxshortdesc'].'</div>';

                echo '<div class="price">';
                if ($article['oxvarcount'] == 0) {
                    if (($article['oxprice'] < $article['oxtprice']) && ($adBanner == 'WideColumn'))
                        echo '<span class="tiltedstrike">&thinsp;<span class="oldprice">'.number_format($article['oxtprice'],2,',','').'</span>&thinsp;</span>&nbsp;';
                    echo '<span class="price">'.number_format($article['oxprice'],2,',','').'&nbsp;&euro;<sup>*</sup></span>';
                    }
                else {
                    echo '<span class="price"><span style="font-size:0.8em;">ab</span> '.number_format($article['oxvarminprice'],2,',','').'&nbsp;&euro;<sup>*</sup></span>';
                    }
                echo '</div>';
                echo '<div class="gotobutton"><button onclick="window.parent.location=\''.$this->sShopURL.'/index.php?cl=details&anid='.$article['oxid'].'\'">Zum Shop</button></div>';
                echo '</div>';
                echo '<div class="clear"></div>';
                echo '</div>';
                }
            echo '<div class="footer" style="top:'.($tableHeight-20).'px;width:'.$tableWidth.'px;">';
            echo '<div class="sepline" style="width:'.($tableWidth-10).'px;"></div>';
            echo '<div class="space"> </div>';
            echo '<div class="pricehint">'
               . '<a href="'.$this->sShopURL.'/Versand-und-Kosten/" target="_blank">'
               . '<sup>*</sup>Alle Preise inkl. MwSt., zzgl. Versandkosten.'
               . '</a></div>';
            echo '</div>';
            }
        else {
            echo '<tr>';
            foreach($artList as $article) {
                echo '<td class="image"><img src="'.$this->sShopURL.'/out/pictures/icon/'.$article['oxicon'].'"></td>';
                echo '<td><span class="title"><a href="'.$this->sShopURL.'/index.php?cl=details&anid='.$article['oxid'].'" target="_blank">'.$article['oxtitle'].'</a></span><br>';
                if (!empty($article['oxshortdesc']))
                        echo '<span class="desc">'.$article['oxshortdesc'].'</span><br>';
                echo '<span class="price">'.number_format($article['oxprice'],2,',','').'&nbsp;&euro;<sup>*</sup></span><br>';
                echo '<button onclick="window.parent.location=\''.$this->sShopURL.'/index.php?cl=details&anid='.$article['oxid'].'\'">Zum Shop</button></td>';
            }
            echo '</tr>';
            echo '<tr><td colspan="8"><span class="pricehint"><a href="'.$this->sShopURL.'/Versand-und-Kosten/" target="_blank"><sup>*</sup>Alle Preise inkl. MwSt., zzgl. Versandkosten.</a></span></td></tr>';
        }


        echo '</div>';
    }
	
}


$actionID = isset($_POST['id']) ? $_POST['id'] : $_GET['id'];
$styleSheet = isset($_POST['s']) ? $_POST['s'] : $_GET['s'];
$bannerFormat = isset($_POST['f']) ? $_POST['f'] : $_GET['f'];
$animMode = isset($_POST['a']) ? $_POST['a'] : $_GET['a'];


//$actionID = '83f69aaf781165afece1b953177e6e95';
//$styleSheet = 'default';
//$bannerFormat = 'WideColumn';
//$animMode = 'r5';
$ads = new WebAds;
$ads->openBanner($styleSheet,$animMode);
$dbh = $ads->openDB();
$artList = $ads->getActionArticles($dbh, $actionID);
$ads->formatAdvertisement($artList, $bannerFormat);
$ads->closeBanner();

?>